package com.CTAtracker.android.Adapters;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Models.StopETA;
import com.CTAtracker.android.XML.InternetETA;
import com.CTAtracker.android.api.CTAResClientUsage;

public class FavoriteStopAdapter extends ArrayAdapter<StopETA> implements Observer {
	/**
	 * The layout to inflate
	 */
	private LayoutInflater _layoutInflater = null;
	/**
	 * The Id of the resource to inflate
	 */
	private int _resourceId = 0;
	/**
	 * The List of the StopETA to adapt to the view
	 */
	private ArrayList<StopETA> _listStopETA;
	/**
	 * CTAResClientUsage
	 */
	private CTAResClientUsage _ctaResClientUsage;
	/**
	 * 
	 * @param context
	 * @param resourceId
	 * @param objects
	 */
	public FavoriteStopAdapter(Context context, int resourceId,
			List<StopETA> objects) {
		super(context, resourceId, 0, objects);

		_layoutInflater = LayoutInflater.from(context);
		_resourceId = resourceId;
		_listStopETA = (ArrayList<StopETA>) objects;
		
		if (isOnline()){
			_ctaResClientUsage = new CTAResClientUsage(this);
			for (int i=0; i<_listStopETA.size(); i++){
				_ctaResClientUsage.getNextArrivalStop(_listStopETA.get(i).get_idStop(), _listStopETA.get(i).getColor().getColorAPI());
				}
		}else {
			// TODO implement network connection
//			Intent intent = new Intent(Intent.ACTION_MAIN);
//			intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
//	    	context.startActivity(intent);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final StopETA stop = _listStopETA.get(position);
		convertView = _layoutInflater.inflate(_resourceId, parent, false);

		TextView tvn = (TextView) convertView.findViewById(R.id.favoriteStopName);
		FrameLayout fl = (FrameLayout) convertView.findViewById(R.id.favoriteFrameColor);
		TextView infoStop = (TextView) convertView.findViewById(R.id.favoriteAutre);
		
		fl.setBackgroundColor(Color.parseColor(stop.getColor().toString()));
		tvn.setText(stop.getNameStopFavorite());

		if (stop.get_listInternetETA().isEmpty()){
			if (isOnline()){
				infoStop.setText("Retrieving Data from the server");
			}else {
				infoStop.setText("Impossible to retrieve data from the server\nPlease Check your connection parameters");
			}
		}else {
			infoStop.setText(stop.getETA());
		}
		
		return convertView;
	}

	@SuppressWarnings("unchecked")
	@Override
	public synchronized void update(Observable observable, Object data) {
		ArrayList<InternetETA> list = (ArrayList<InternetETA>) data;
		
		for (int i=0; i<_listStopETA.size(); i++){
			_listStopETA.get(i).get_listInternetETA().clear();
		}
		
		for (int j=0; j< _listStopETA.size(); j++){
			for (int i=0; i<list.size(); i++){
				if (list.get(i).get_stpId() == _listStopETA.get(j).get_idStop()){
					_listStopETA.get(j).get_listInternetETA().add(list.get(i));
				}else {
					// Do nothing
				}	
			}
		}
		this.notifyDataSetChanged();
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm =
	        (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}

}
